<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class ConfigController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    //站点配置
    public function siteconfig()
    {
        $config = $this->getConfig("siteconfig");
        return view("admins.config.siteconfig")->with('config',$config);
    }
    //基本信息配置界面
    public function information()
    {
        $config = $this->getConfig("information");
        return view("admins.config.information")->with('config',$config);
    }
    //百度推送配置界面
    public function baidu()
    {
        $config = $this->getConfig("baidu");
        return view("admins.config.baidu")->with('config',$config);
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //验证的调用
        $this->checkdata($request->name,$request);
        //数据入库
        $data = $request->only('name','title');
        $config = json_encode($request->except('name','title','_token'));
        $data['config']=$config;
        $data['created_at']=date("Y/m/d H:i:s",time());
        $data['updated_at']=date("Y/m/d H:i:s",time());
        $config = DB::table('configs')->where('name', '=', $request->name)->first();
        if($config){
            $result = DB::table('configs')->where('name', '=', $request->name)->update($data);
        }else{
            $result = DB::table('configs')->insert($data);
        }
        if($result===true || $result>0){
            session()->flash("data",['class'=>'success','msg'=>'更新数据成功！']);  
        }else{
            session()->flash("data",['class'=>'danger','msg'=>'更新数据失败！']);
        }
        return redirect(route('admin.config.'.$request->name));
    } 
    //根据配置标识获取信息
    private function getConfig($name = "siteconfig"){
        $res_config = DB::table("configs")->where('name', '=', $name)->first();
        $config=[]; 
        if($res_config){
            $config = json_decode($res_config->config,true);
        }
        return $config;
    }
    private function checkdata($name = "siteconfig", $request){
        switch ($name){
            case "information":
                //验证
                $datavalidate = $request->validate([
                    'company' => 'required',
                    'address' => 'required',
                    'phone' => 'required'
                ],[
                    'company.required'=>"公司名称不能空",
                    'address.required'=>"公司地址不能空",
                    'phone.required'=>"客户热线不能空"
                ]);
            break;
            case "baidu":
                //验证
                $datavalidate = $request->validate([
                    'key' => 'required'
                ],[
                    'key.required'=>"百度密钥不能空"
                ]);
            break;
            default:
                //验证
               $datavalidate = $request->validate([
                   'name' => 'required',
                   'title' => 'required',
                   'sitetitle' => 'required',
                   'domain' => 'required|url'
               ],[
                   'name.required'=>"配置标识不能空",
                   'title.required'=>"配置名称不能空",
                   'sitetitle.required'=>"网站名称不能空",
                   'domain.required'=>"网站域名不能空",
                   'domain.url'=>'网址输入不正确'
               ]);
            break;  
        }
    }
    
}
